package com.zy.mall.dao;

import com.alibaba.druid.util.StringUtils;
import com.zy.mall.model.Admin;
import com.zy.mall.model.bo.AdminChangeBo;
import com.zy.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;


import java.sql.SQLException;
import java.util.*;

public class AdminDaoImpl implements AdminDao {
    public Admin login(Admin admin) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        Admin query = null;
        try {
            query = runner.query("select * from admin where email = ? and pwd = ?",
                    new BeanHandler<>(Admin.class),
                    admin.getEmail(),
                    admin.getPwd());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    @Override
    public List<Admin> allAdmins() {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<Admin> admins = null;
        try {
            admins = runner.query("select * from admin", new BeanListHandler<Admin>(Admin.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admins;
    }


    /**
     * 动态SQL
     * @param admin
     * @return
     */
    @Override
    public List<Admin> getSearchAdmins(Admin admin) {
        Map<String, Object> result = getDynamicSql(admin);
        String sql = (String) result.get("sql");
        List<String> params = (List<String>) result.get("params");
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<Admin> admins = null;
        try {
            admins = runner.query(sql, new BeanListHandler<Admin>(Admin.class), params.toArray());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admins;
    }

    @Override
    public void addAdmin(Admin admin) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            runner.update("insert into admin values(null, ?, ?, ?)",
                    admin.getEmail(),
                    admin.getNickname(),
                    admin.getPwd());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean deleteAdmins(String id) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            int update = runner.update("delete from admin where id = ?", Integer.parseInt(id));
            if (update == 1) return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean updateAdmins(Admin admin) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            int update = runner.update("UPDATE admin SET email = ?, nickname = ?, pwd = ? where id = ?",
                    admin.getEmail(), admin.getNickname(), admin.getPwd(), admin.getId());
            if (update == 1) return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }


    @Override
    public Admin getAdminsInfo(String id) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        Admin query = null;
        try {
            query = runner.query("select * from admin where id = ?",
                    new BeanHandler<>(Admin.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    @Override
    public int changePwd(AdminChangeBo changeBo) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        Admin query = null;
        try {
            query = runner.query("select * from admin where email = ?",
                    new BeanHandler<>(Admin.class), changeBo.getAdminToken());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (query.getPwd().equals(changeBo.getOldPwd())) {
            if (!changeBo.getNewPwd().equals(changeBo.getConfirmPwd())) {
                return 500;
            }
            try {
                int update = runner.update("UPDATE admin SET pwd = ? where email = ?",
                        changeBo.getNewPwd(), changeBo.getAdminToken());
                return 0;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return 10000;
    }


    private Map<String, Object> getDynamicSql(Admin admin) {
        String base = "select * from admin where 1 = 1";
        List<String> params = new ArrayList<>();
        if(!StringUtils.isEmpty(admin.getEmail())){
            base = base + " and email like ?";
            params.add("%" + admin.getEmail() + "%");
        }
        if(!StringUtils.isEmpty(admin.getNickname())){
            base = base + " and nickname like ?";
            params.add("%" + admin.getNickname() + "%");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("sql", base);
        map.put("params", params);
        return map;
    }
}
